import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/login/register'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页管理', icon: 'dashboard' }
    }]
  },

  {
    path: '/categories',
    component: Layout,
    children: [{
      path: '',
      name: 'Categories',
      component: () => import('@/views/category/index'),
      meta: { title: '分类管理', icon: 'table' }
    }]
  },

  {
    path: '/pages',
    component: Layout,
    children: [{
      path: '',
      name: 'Pages',
      component: () => import('@/views/page/index'),
      meta: { title: '页面管理', icon: 'table' }
    }]
  },

  {
    path: '/agents',
    component: Layout,
    children: [{
      path: '',
      name: 'Agents',
      component: () => import('@/views/agent/index'),
      meta: { title: '经纪管理', icon: 'people' }
    }]
  },

  {
    path: '/client',
    component: Layout,
    children: [{
      path: '',
      name: 'Clients',
      component: () => import('@/views/client/index'),
      meta: { title: '客户管理', icon: 'people' }
    }]
  },

  {
    path: '/property',
    component: Layout,
    children: [{
      path: '',
      name: 'Properties',
      component: () => import('@/views/property/index'),
      meta: { title: '房产管理', icon: 'people' }
    }]
  },

  {
    path: '/lottery',
    name: 'Lottery',
    component: () => import('@/views/lottery/index'),
    hidden: true
  },

  {
    path: '/permissions',
    component: Layout,
    children: [{
      path: '',
      name: 'Permissions',
      component: () => import('@/views/permissions/index'),
      meta: { title: '权限管理', icon: 'table' }
    }]
  },

  {
    path: '/roles',
    component: Layout,
    children: [{
      path: '',
      name: 'Roles',
      component: () => import('@/views/roles/index'),
      meta: { title: '角色管理', icon: 'table' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
